<template>
  <div class="login">
    <div class="login-wrapper">
      <el-card class="login-card" shadow="always">
        <template #header>
          <div class="login-header">
            <el-icon size="48"><User /></el-icon>
            <h1>管理员登录</h1>
            <p>欢迎回来！请登录您的账号</p>
          </div>
        </template>

            <el-form
              ref="loginFormRef"
              :model="formData"
              :rules="formRules"
              size="large"
              @submit.prevent="handleLogin"
            >
              <el-form-item prop="username">
                <el-input
                  v-model="formData.username"
                  placeholder="请输入用户名"
                  :prefix-icon="User"
                  :disabled="loading"
                  clearable
                />
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                  v-model="formData.password"
                  type="password"
                  placeholder="请输入密码"
                  :prefix-icon="Lock"
                  :disabled="loading"
                  show-password
                  clearable
                />
              </el-form-item>

              <el-form-item>
                <el-button
                  type="primary"
                  size="large"
                  class="login-btn"
                  :loading="loading"
                  :disabled="!isFormValid"
                  @click="handleLogin"
                >
                  {{ loading ? '登录中...' : '登录' }}
                </el-button>
              </el-form-item>

              <el-alert
                v-if="errorMessage"
                :title="errorMessage"
                type="error"
                show-icon
                :closable="false"
                style="margin-bottom: 20px;"
              />
            </el-form>
          </el-card>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const errorMessage = ref('')

// 表单数据
const formData = reactive({
  username: 'admin',    // 默认管理员账号
  password: 'admin123', // 默认管理员密码
})

// 表单验证规则
const formRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名至少3个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6个字符', trigger: 'blur' }
  ]
}

// 表单验证
const isFormValid = computed(() => {
  return formData.username.length >= 3 && formData.password.length >= 6
})

// 处理登录
async function handleLogin() {
  if (!isFormValid.value) {
    return
  }

  loading.value = true
  errorMessage.value = ''

  try {
    // 模拟登录延迟
    await new Promise(resolve => setTimeout(resolve, 1000))

    // 验证账号密码
    if (formData.username === 'admin' && formData.password === 'admin123') {
      // 登录成功
      ElMessage.success('登录成功！')

      // 保存登录状态到 localStorage
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('userInfo', JSON.stringify({
        username: 'admin',
        role: 'admin',
        loginTime: new Date().toISOString(),
      }))

      // 跳转到管理员页面
      await router.push('/dashboard')
    } else {
      errorMessage.value = '用户名或密码错误！'
      ElMessage.error('用户名或密码错误！')
    }
  } catch (error) {
    console.error('Login error:', error)
    errorMessage.value = '登录失败，请重试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login {
  min-height: 100vh;
  width: 100vw;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #69b7ff 0%, #91d5ff 100%);
  padding: 20px;
  box-sizing: border-box;
}

.login-wrapper {
  width: 100%;
  max-width: 600px;
  display: flex;
  justify-content: center;
}

.login-card {
  width: 100%;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(105, 183, 255, 0.15);
  overflow: hidden;
}

.login-header {
  text-align: center;
  padding: 32px 32px 0 32px;
  background: linear-gradient(135deg, #69b7ff 0%, #91d5ff 100%);
  color: white;
  margin-bottom: 0;
}

.login-header h1 {
  font-size: 24px;
  font-weight: 700;
  color: white;
  margin: 0 0 8px 0;
}

.login-header p {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  margin: 0 0 32px 0;
}

.login-form {
  width: 100%;
  padding: 32px;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  background: linear-gradient(135deg, #69b7ff 0%, #91d5ff 100%);
  border: none;
  margin-bottom: 24px;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(105, 183, 255, 0.4);
}

.login-footer {
  text-align: center;
  color: #666;
  font-size: 14px;
  padding: 0 32px 32px 32px;
}

.link {
  color: #69b7ff;
  text-decoration: none;
  font-weight: 500;
  margin-left: 0.5rem;
}

.link:hover {
  text-decoration: underline;
}

/* 输入框样式优化 */
.login-form :deep(.el-form-item) {
  margin-bottom: 24px;
}

.login-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  height: 48px;
}

.login-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.login-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 12px rgba(105, 183, 255, 0.3);
}

/* 动画效果 */
.login-card {
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login {
    padding: 16px;
  }

  .login-card {
    max-width: 100%;
  }

  .login-header {
    padding: 24px 24px 0 24px;
  }

  .login-header h1 {
    font-size: 20px;
  }

  .login-form {
    padding: 24px;
  }

  .login-footer {
    padding: 0 24px 24px 24px;
  }
}
</style>
